# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:张广勤
@Web site: https://www.tunan.wang
@Github:www.github.com
 
@File:Yi3_0.py
@Time:2024/8/7 16:15

@Motto:不积跬步无以至千里，不积小流无以成江海！
"""

import random

import time

def coin_toss_to_trigram(toss):
    """
    将三枚铜钱的结果转换为卦象（少阳、老阳、少阴、老阴）
    :param toss: 投掷结果列表，包含三个'H'或'T'
    :return: 卦象字符串
    """
    counts = toss.count('H'), toss.count('T')
    if counts == (3, 0):
        return "老阳 ——", 1
    elif counts == (2, 1):
        return "少阴 --", 0
    elif counts == (1, 2):
        return "少阳 ——", 1
    else:
        return "老阴 --", 0

# 初始化记录每次投掷结果的列表
results = []

# 投掷六次
for _ in range(6):
    # 初始化单次投掷的结果列表
    single_throw = ['H' if random.random() < 0.5 else 'T' for _ in range(3)]
    # 转换为卦象
    trigram = coin_toss_to_trigram(single_throw)
    # 记录投掷结果和卦象
    results.append((single_throw, trigram))

gua=[]
# 反向打印每次投掷的结果和卦象
for i, (throw, trigram) in reversed(list(enumerate(results, 1))):
    print(f"投掷{i}: {' '.join(throw)} -> {trigram}")
    # print(f"{trigram[1]}")
    gua.append(trigram[1])
    time.sleep(2)

# print(gua[::-1])
gua.reverse()
print(gua)

result = ''.join(map(str,gua))
print(result)
# print(type(result))

# 转换老阳和老阴
converted_gua = []
for _, trigram in results:
    if trigram[0] == "老阳 ——":
        converted_gua.append("老阴 --")
    elif trigram[0] == "老阴 --":
        converted_gua.append("老阳 ——")
    else:
        converted_gua.append(trigram[0])

    # 打印转换后的结果
print("转换后的卦象:")
for i, gua in enumerate(reversed(converted_gua), 1):
    print(f"爻{i}: {gua}")


converted_gua.reverse()
print(converted_gua)

result = ''.join(map(str,converted_gua))
print(result)
# 如果你需要将转换后的结果作为一个字符串
# result = ''.join(gua[0] for gua in converted_gua)  # 注意：这里假设gua[0]是字符串
# print("转换后的卦象字符串（不含爻序号）:")
# print(result)